package com.example.musicdemo1.service

import android.media.MediaPlayer
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.musicdemo1.model.PlaylistItem
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

object MusicPlayer {
    private val mediaPlayer = MediaPlayer()
    private val _currentMusic = MutableStateFlow<PlaylistItem?>(null)
    private val _isPlaying = MutableStateFlow(false)
    private val _currentPosition = MutableStateFlow(0)
    private var playlist: List<PlaylistItem> = emptyList()
    private var currentIndex = -1
    
    val currentMusic: StateFlow<PlaylistItem?> = _currentMusic
    val isPlaying: StateFlow<Boolean> = _isPlaying
    val currentPosition: StateFlow<Int> = _currentPosition

    init {
        mediaPlayer.setOnCompletionListener {
            _isPlaying.value = false
            playNext()
        }
    }

    fun setPlaylist(newPlaylist: List<PlaylistItem>) {
        playlist = newPlaylist
        if (_currentMusic.value == null && playlist.isNotEmpty()) {
            val defaultSong = playlist.find { it.title == "甜蜜蜜" }
            defaultSong?.let {
                currentIndex = playlist.indexOfFirst { song -> song.id == it.id }
                _currentMusic.value = it
                prepareMusic(it)
            }
        }
    }

    fun setCurrentMusic(music: PlaylistItem) {
        currentIndex = playlist.indexOfFirst { it.id == music.id }
        _currentMusic.value = music
    }

    fun play(music: PlaylistItem) {
        currentIndex = playlist.indexOfFirst { it.id == music.id }
        _currentMusic.value = music
        playMusic(music)
    }

    private fun playMusic(music: PlaylistItem) {
        try {
            mediaPlayer.reset()
            mediaPlayer.setDataSource(music.src)
            mediaPlayer.prepare()
            mediaPlayer.start()
            _currentMusic.value = music
            _isPlaying.value = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun playNext() {
        if (playlist.isEmpty() || currentIndex == -1) return
        currentIndex = (currentIndex + 1) % playlist.size
        playMusic(playlist[currentIndex])
    }

    fun playPrevious() {
        if (playlist.isEmpty() || currentIndex == -1) return
        currentIndex = if (currentIndex > 0) currentIndex - 1 else playlist.size - 1
        playMusic(playlist[currentIndex])
    }

    fun pause() {
        if (mediaPlayer.isPlaying) {
            mediaPlayer.pause()
            _isPlaying.value = false
        }
    }

    fun resume() {
        if (_currentMusic.value != null && !mediaPlayer.isPlaying) {
            try {
                mediaPlayer.start()
                _isPlaying.value = true
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    fun seekTo(position: Int) {
        mediaPlayer.seekTo(position)
    }

    fun getDuration(): Int = mediaPlayer.duration

    fun getCurrentPosition(): Int = mediaPlayer.currentPosition

    fun prepareMusic(music: PlaylistItem) {
        try {
            mediaPlayer.reset()
            mediaPlayer.setDataSource(music.src)
            mediaPlayer.prepare()
            _currentMusic.value = music
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
} 